#include "../../Application/Application.h"
#include "WindowImpl.h"

namespace GUI
{
	namespace Win32
	{
		WindowImpl::WindowImpl(const std::string & _strTitle, int _iWidth, int _iHeight)
			: WidgetImpl(), m_strTitle(_strTitle)
		{
			m_iWidth = _iWidth;
			m_iHeight = _iHeight;
		}

		WindowImpl::~WindowImpl()
		{
		}
		
		const std::string & WindowImpl::GetTitle() const
		{
			return m_strTitle;
		}

		HWND WindowImpl::CreateHandle()
		{
			HINSTANCE _hInstance = GetModuleHandle(NULL);

			RECT rc = { 0, 0, m_iWidth, m_iHeight };
			AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);

			WNDCLASSEX wcex;
			wcex.cbSize = sizeof(WNDCLASSEX);
			wcex.style = CS_HREDRAW | CS_VREDRAW;
			wcex.lpfnWndProc = Application::WndProc;
			wcex.cbClsExtra = 0;
			wcex.cbWndExtra = 0;
			wcex.hInstance = _hInstance;
			wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
			wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
			wcex.hbrBackground = CreateSolidBrush(RGB(240, 240, 240));
			wcex.lpszMenuName = NULL;
			wcex.lpszClassName = m_strTitle.c_str();
			wcex.hIconSm = NULL;
			if (!RegisterClassEx(&wcex))
			{
				return NULL;
			}

			HWND _hWnd = CreateWindowEx(NULL, m_strTitle.c_str(), m_strTitle.c_str(), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, _hInstance, NULL);
			if (!_hWnd)
			{
				return NULL;
			}

			//ShowWindow(_hWnd, SW_SHOWDEFAULT); // genere un evenement WM_SIZE
			//UpdateWindow(_hWnd);

			return _hWnd;
		}
	}
}